package com.itheima.service;

import com.itheima.pojo.Movie;
import com.itheima.pojo.ShowMovie;

import java.util.ArrayList;
import java.util.Scanner;

public class MovieManger {

    ArrayList<Movie> list = new ArrayList<>();
    Scanner sc = new Scanner(System.in);
    ArrangementService arrange = new ArrangementService();



    public void movieManagerStart(ArrayList<ShowMovie> showMovies) {
        System.out.println("=========电影管理管理==========");
        while (true) {
            System.out.println("请选择以下功能:");
            System.out.println("\r\t1.电影浏览\t2.新片上架\t3.电影下架\t4.修改电影\t5.排片设置\t6.退出");
            String choice = sc.next();

            switch (choice) {
                case "1":
                    movieBrose();
                    break;
                case "2":
//                    newMovie();
                    test();
                    break;
                case "3":
                    System.out.println(movieDelete());
                    break;
                case "4":
                    updateMovie();
                    break;
                case "5":
                    arrange.ArrangeStart(list,showMovies);
                    break;
                case "6":
                    System.out.println("退出电影管理!");
                    return;
                default:
                    System.out.println("你输入的信息有误,请重新输入!");
            }
        }
    }

    //
    public void newMovie() {

        String s = "";
        System.out.println("===========新片上架===========");
        System.out.println("请输入电影名称:");
        String next = sc.next();
        for (int i = 0; i < list.size(); i++) {
            if (next.equals(list.get(i).getName())) {
                System.out.println("电影重复,请重新输入!");
                return;
            }
        }
        System.out.println("请输入电影导演:");
        String next1 = sc.next();
        System.out.println("请输入电影简介:");
        String next2 = sc.next();
        System.out.println("请输入电影时长:");
        int i = sc.nextInt();
        while (true) {
            System.out.println("请输入电影主演:");
            String next3 = sc.next();
            s += "," + next3;
            System.out.println("请选择:1.继续\t2.结束");
            String i1 = sc.next();
            if ("1".equals(i1)) {
            } else if ("2".equals(i1)) {
                break;
            }
        }
        String substring = s.substring(1, s.length());
        Movie movie = new Movie(next, next1, next2, i, substring);
        list.add(movie);
        System.out.println("电影上架成功!");

    }

    public void movieBrose() {
        System.out.println("==========电影浏览==========");
        if (list.size() == 0) {
            System.out.println("还没上架任何电影,请添加电影后重试!");
            return;
        }

        for (int i = 0; i < list.size(); i++) {
            if (i == list.size() - 1) {
                System.out.println(list.get(i));
            } else {
                System.out.println(list.get(i));
                System.out.println("----------------------");
            }
        }
    }

    public String movieDelete() {
        System.out.println("===========新片下架===========");
        System.out.println("请输入电影名称:");
        String next = sc.next();
        for (int i = 0; i < list.size(); i++) {
            if (next.equals(list.get(i).getName())) {
                list.remove(i);
                return "电影下架成功!";
            }
        }
        return "该电影不存在,请检查后重试!";

    }

    public void updateMovie() {
        System.out.println("请输入要修改的电影的名称");
        String next = sc.next();
        for (Movie movie : list) {
            if (next.equals(movie.getName())) {
                update(movie);
                return;
            }
        }
        System.out.println("你输入的电影不存在,请重新选择!");

    }

    public void update(Movie movie) {
        StringBuilder sb = new StringBuilder();


        while (true) {
            System.out.println("请选择需要修改的选项:1.名字\t2.导演\t3.简介\t4.时长\t5.主演\t6.退出");
            String choice = sc.next();
            loop:
            switch (choice) {
                case "1":
                    System.out.println("请输入要修改的名字为:");
                    String next = sc.next();
                    movie.setName(next);
                    break;
                case "2":
                    System.out.println("请输入要修改的导演为:");
                    String next1 = sc.next();
                    movie.setDirector(next1);
                    break;
                case "3":
                    System.out.println("请输入要修改的简介为:");
                    String next2 = sc.next();
                    movie.setProfile(next2);
                    break;
                case "4":
                    System.out.println("请输入要修改的时长为:");
                    int next3 = sc.nextInt();
                    movie.setDuration(next3);
                    break;
                case "5":
                    String[] split = movie.getActor().split(",");
                    System.out.println("请输入想要修改的主演为:");
                    String next4 = sc.next();
                    for (int i = 0; i < split.length; i++) {
                        if (next4.equals(split[i])) {
                            System.out.println("修改后的主演为:");
                            String next5 = sc.next();
                            split[i] = next5;
                            for (int j = 0; j < split.length; j++) {
                                sb.append(split[j]);
                                if (j != split.length - 1) {
                                    sb.append(",");
                                }
                            }
                        }
                        movie.setActor(sb.toString());
                        break loop;
                    }
                    System.out.println("你输入的演员不存在,请重新选择!");
                    break;



                case "6":
                    return;
            }
        }
    }


    public void test(){
        Movie movi1 = new Movie("卧虎藏龙","李安","很好的故事",2,"刘德华,章子怡");
        Movie movi2 = new Movie("肖申克的救赎","弗兰克·德拉邦特","一场谋杀案使银行家安迪（蒂姆•罗宾斯 Tim Robbins 饰）蒙冤入狱，" +
                "谋杀妻子及其情人的指控将囚禁他终生。在肖申克监狱的首次现身就让监狱“大哥”瑞德（摩根•弗里曼 Morgan Freeman 饰）对他另眼相看。" +
                "瑞德帮助他搞到一把石锤和一幅女明星海报，两人渐成患难之交。",2,"蒂姆·罗宾斯,摩根·弗里");

        Movie movi3 = new Movie("霸王别姬","陈凯歌","段小楼（张丰毅）与程蝶衣（张国荣）是一对打小一起长大的师兄弟，" +
                "两人一个演生，一个饰旦，一向配合天衣无缝，尤其一出《霸王别姬》，更是誉满京城，" +
                "为此，两人约定合演一辈子《霸王别姬》。但两人对戏剧与人生关系的理解有本质不同，段小楼深知戏非人生，程蝶衣则是人戏不分。",2,"张国荣,张丰毅");

        list.add(movi1);
        list.add(movi2);
        list.add(movi3);
    }
}
